﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
//using Microsoft.SharePoint.Workflow;
//using System.Workflow.ComponentModel;

namespace CNRS.CCP.Connector
{
	public enum LogCategorie
	{
		SecurityHelper,
		FeatureHelper,
		MappingHelper,
		ClaimsHelper,
		ClaimsProvider,
		LdapHelper,
		SqlHelper,
		VootHelper,
		ConfigLoader
	}

	// http://www.codeproject.com/Articles/620996/Five-suggestions-to-implement-a-better-logging-in
	// to set up logging for SP
	// and http://www.wictorwilen.se/Post/Improve-your-SharePoint-2010-applications-with-monitoring-using-SPMonitoredScope.aspx to manage dashboard
	// http://www.nothingbutsharepoint.com/2012/08/15/how-to-log-to-the-sharepoint-uls-logs-clean-debugging-and-error-logging-broken-down-into-steps-aspx-2/
	//
	/// <summary>
   


	/// <summary>
	/// Logging into ULS
	/// from http://www.nothingbutsharepoint.com/2012/08/15/how-to-log-to-the-sharepoint-uls-logs-clean-debugging-and-error-logging-broken-down-into-steps-aspx-2/
	/// from http://www.codeproject.com/Articles/620996/Five-suggestions-to-implement-a-better-logging-in
	/// </summary>
	public class LogHelper : SPDiagnosticsServiceBase
	{
		public const string PRODUCT_NAME = "CNRSccp";
		private static LogHelper _Current;
		
		public static LogHelper Current
		{
			get
			{
				if (_Current == null)
				{
					_Current = new LogHelper();
				}
				return _Current;
			}
		}
 
		private LogHelper()
			: base(PRODUCT_NAME, SPFarm.Local)
		{
		}
 
		//TODO : still have to look at how traceSeverity and EventSeverity works.
		protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
		{
			List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>        
			{            
				new SPDiagnosticsArea(PRODUCT_NAME, new List<SPDiagnosticsCategory>            
				{                
					new SPDiagnosticsCategory("Error", TraceSeverity.High, EventSeverity.Error),
					new SPDiagnosticsCategory("Warning", TraceSeverity.Medium, EventSeverity.Warning),
					new SPDiagnosticsCategory("Logging", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("Debugging", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("SecurityHelper", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("FeatureHelper", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("MappingHelper", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("ClaimsHelper", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("ClaimsProvider", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("LdapHelper", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("SqlHelper", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("VootHelper", TraceSeverity.Verbose, EventSeverity.Verbose),
					new SPDiagnosticsCategory("ConfigLoader", TraceSeverity.Verbose, EventSeverity.Verbose)
				})        
			};
			return areas;
		}
 
		private string MapTraceSeverity(TraceSeverity traceSeverity)
		{
			switch (traceSeverity)
			{
				case TraceSeverity.High: return "Error";
 
				case TraceSeverity.Medium: return "Warning";
 
				default:
				case TraceSeverity.Verbose:
					return "Debugging";
			}
		}

		/// <summary>
		/// Initial method signature
		/// </summary>
		/// <param name="categorie"></param>
		/// <param name="message"></param>
		/// <param name="severity"></param>
		public static void Log(LogCategorie categoryName, string message, TraceSeverity severity)
		{
			//SPDiagnosticsCategory category = new SPDiagnosticsCategory(categorie.ToString(), TraceSeverity.Monitorable, EventSeverity.Error);
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories[categoryName.ToString()];			
			LogHelper.Current.WriteTrace(0, category , TraceSeverity.High, message);

		}

		public static void Log(TraceSeverity traceSeverity, Exception ex)
		{
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories["Error"];
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, ex.Message);
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, ex.ToString());
		}
 
		public static void Log(TraceSeverity traceSeverity, string message, Exception ex)
		{
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories["Error"];
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, ex.Message);
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, ex.ToString());
		}
 
		public static void LogError(Exception ex)
		{
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories["Error"];
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, ex.Message);
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, ex.ToString());
		}
 
		public static void LogError(Exception ex, string message)
		{
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories["Error"];
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, ex.Message);
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, ex.ToString());
		}
 
		public static void LogError(string message, string stackTrace)
		{
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories["Error"];
			LogHelper.Current.WriteTrace(0, category, TraceSeverity.High, message);
		}
 
		public static void LogWarning(string message)
		{
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories["Warning"];
			LogHelper.Current.WriteTrace(1, category, TraceSeverity.Medium, message);
		}
 
		public static void LogMessage(string message)
		{
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories["Logging"];
			LogHelper.Current.WriteTrace(1, category, TraceSeverity.Verbose, message);
		}
 
		public static void LogDebug(LogCategorie categoryName, string message)
		{
			SPDiagnosticsCategory category = LogHelper.Current.Areas[PRODUCT_NAME].Categories[categoryName.ToString()];
			LogHelper.Current.WriteTrace(1, category, TraceSeverity.Verbose, message);
		}
 
	}
}
